package com.acme.tcp;

import java.io.Serializable;
import java.util.StringTokenizer;

import com.acme.domain.account.Account;
import com.acme.domain.account.CheckingAccount;
import com.acme.domain.account.SavingAccount;
import com.acme.domain.bank.Bank;
import com.acme.domain.bank.Client;
import com.acme.domain.bank.Gender;

public class BankReport implements Serializable{

	private static final long serialVersionUID = 1L;
	private String accType;
	private String clientName;
	private Gender gender;
	private double balance;
	private double overdraft;
	private int clientId;
	
	
	public Bank addClientFromFeed(final Bank bank, final String feed){
		StringTokenizer st = new StringTokenizer(feed, "=;");
		clientId++;
		while (st.hasMoreTokens()) {
			String key = st.nextToken();
			if (key.equals("accounttype")) {
				if (st.nextToken().equals("c")) {
					accType = "checking";
				} else {
					accType = "saving";
				}

			}

			if (key.equals("balance")) {
				balance = Double.parseDouble(st.nextToken());
			}

			if (key.equals("overdraft")) {
				overdraft = Double.parseDouble(st.nextToken());
			}
			if (key.equals("name")) {
				clientName = st.nextToken();
			}
			if (key.equals("gender")) {
				if (st.nextToken().equals("m")) {
					gender = Gender.MALE;
				} else {
					gender = Gender.FEMALE;
				}
			}

		}
		Client client = new Client(clientName, gender);
		Account clientAcc = null;
		if (accType.equals("checking")) {
			clientAcc = new CheckingAccount(clientId, balance, overdraft);
		} else {
			clientAcc = new SavingAccount(clientId, balance);
		}

		bank.addClient(client);
		client.addAccount(clientAcc);
		return bank;
	}
	
}
